package demo3;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

public class MySingleList {

    static class ListNode {
        public int val;
        public ListNode next;//存储下一个节点的地址
        //构造方法
        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head = null;

    //创建链表
    public void createList(){
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;

    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
        } else {
            node.next = head;
            head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null){
            head = node;
        } else {
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //展示
    public void disply(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //从指定节点开始打印 暂时先放着看看
    public void display(ListNode newHead){
        ListNode cur = newHead;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //任意位置插入
    public void addIndex(int index, int data) throws MySingleListIndexOutOfException{
        checkIndexAdd(index);//判断是否出界
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndexSubOne(index);
        node.next = cur.next;
        cur.next = node;
    }

    //找到index-1位置的节点
    private ListNode findIndexSubOne(int index){
        ListNode cur = this.head;
        while(index - 1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public void checkIndexAdd(int index){
        if(index < 0 || index > size()){
            throw new MySingleListIndexOutOfException("任意位置插入的时候，index不合法！");
        }
    }

    public int size(){
        ListNode cur = head;
        int cnt = 0;
        while(cur != null){
            cnt ++;
            cur = cur.next;
        }
        return cnt;
    }

    public boolean contain(int key){
        ListNode cur = head;
        while(cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现的为key的节点
    public void remove(int key){
        if(head == null){
            System.out.println("链表为空");
            return;
        }

        if(this.head.val == key){
            this.head = this.head.next;
            return;
        }

        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                if(cur.next.next != null){
                    cur.next = cur.next.next;
                } else {
                    cur.next = null;
                }
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode cur = head.next;
        ListNode prev = head;

        //while这部分全都是head后面的节点 不包括head这个节点
        while(cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        //单独处理了头结点
        if (head.val == key){
            head = head.next;
        }
    }

    //将所有的的节点回收
    public void clear() {
        ListNode cur = head;
        ListNode curNext = null;
        while(cur != null){
            curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }

    //反转链表 返回新的头结点
    public ListNode reverseList(){
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    //返回中间节点，如果有两个中间节点就返回第二个中间节点
    //使用快慢指针
    public ListNode middleNode (){
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //输出倒数第k个节点
    public ListNode findKthToTail(int k){
        if(k <= 0){
            return null;
        }
        if(head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(k - 1 > 0){
            fast = fast.next;
            //超出范围
            if (fast == null){
                return null;
            }
            k--;
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    public boolean hasCycle() {
        if (head == null){
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }

    public void createLoop(){
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = head.next.next;
    }

    public ListNode partition(int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = head;
        while(cur != null) {
            if(cur.val < x) {
                //判断是不是第一次插入
                if(bs == null) {
                    bs = cur;
                    be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if(as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null){
            //说明第一个区域没有数据
            return as;
        }
        be.next = as;
        if(as != null){
            ae.next = null;
        }
        return bs;
    }
    //回文
    public boolean chePalindrome(){
        if(head == null) return false;
        if(head.next == null) return true;//只有一个节点

        //找中间节点 利于反转
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        //slow一定是中间节点 反转
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = curNext;
            cur = curNext;
        }

        //slow走到最后一个节点
        while (head != slow){
            if (head.val != slow.val){
                return false;
            }
            //偶数的情况
            if (head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public ListNode detectCycle(ListNode head){
        if(head == null) return null;
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        //到这里就有两种情况
        // 不满足循环条件 其中一个有null 没有环
        //遇到了break; 有环 且相遇
        if (fast == null || fast.next == null) {
            return null;
        }
        slow = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }


}
